package org.spindle.dungeon.entities;

import org.newdawn.slick.*;
import org.spindle.dungeon.Constants;
import org.spindle.dungeon.EntityManager;
import org.spindle.dungeon.LevelProperties;

import java.util.ArrayList;
import java.util.List;

public abstract class GeneralEntity {

    //Position fields
    public int x = 10;
    public int y = 10;
    public int direction = 0;
    public int hitBoxWidth = Constants.TILE_SIZE;
    public int hitBoxHeight = Constants.TILE_SIZE;
    public int offsetHitboxX = 0;
    public int offsetHitboxY = 0;

    //Entity type
    public short type = EntitiesConstants.GENERAL_TYPE;


    //Presentation fields
    public Animation[] animations = new Animation[4];
    public List<Image> spritesList = new ArrayList<Image>();
    public int spriteIndex = 0;
    public boolean staticImage = false;

    //Smaller is higher
    public Integer layer = 10;

    //Is collidable?
    public boolean collidable = true;

    //Can be triggered
    public boolean triggerable = false;
    public ArrayList<GeneralEntity> triggerables = new ArrayList<GeneralEntity>();

    //Entity lifetime = -1 is infinite lifetime at 0 will be removed
    public Integer lifeTime = -1;


    public boolean hurt = false;

    //Is a weapon
    public Boolean weapon = false;

    protected GeneralEntity() {
        initialize();
    }

    public void render(LevelProperties props, Graphics g) {
        if (!hurt) {
            if (staticImage) {
                spritesList.get(spriteIndex).draw(x - props.cameraX, y - props.cameraY);
            } else {
                animations[direction].draw(x - props.cameraX, y - props.cameraY);
            }
        } else {
            if (staticImage) {
                spritesList.get(spriteIndex).draw(x - props.cameraX, y - props.cameraY,  Color.red);
            } else {
                animations[direction].draw(x - props.cameraX, y - props.cameraY, Color.red);
            }
            hurt = false;
        }

        if (Constants.debug) {
            g.drawRect(x - props.cameraX + offsetHitboxX, y - props.cameraY + offsetHitboxY, hitBoxWidth, hitBoxHeight);
        }
    }

    protected abstract void initialize();

    public abstract void update(GameContainer gc, LevelProperties props);

    protected Image[] getAnimationArray(int fromX, int fromY, int num) {
        Image[] images = new Image[num];

        for (int i = 0; i < num; i++) {
            images[i] = Constants.SPRITE_SHEET.getSprite(fromX + i, fromY);
        }

        return images;
    }

    public void trigger(GeneralEntity by) {

    }

    ;

    protected boolean doGeneralAIDirectionMove() {
        boolean moved = false;
        switch (direction) {
            case 0:
                if (canMove(0, -1)) {
                    y--;
                    moved = true;
                }
                break;
            case 1:
                if (canMove(0, 1)) {
                    y++;
                    moved = true;
                }
                break;
            case 2:
                if (canMove(-1, 0)) {
                    x--;
                    moved = true;
                }
                break;
            case 3:
                if (canMove(1, 0)) {
                    x++;
                    moved = true;
                }
                break;
        }

        return moved;
    }

    protected boolean canMove(int x, int y) {
        return EntityManager.getInstance().canMove(this, x, y);
    }

    public void updateBy(Object map, int type) {

    }

    public void reset() {

    }

}
